﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrainManage.Server.Factory;
using GrainManage.Entities;
using GrainManage.Entities.District;
using System.Data;
using GrainManage.Entities;
using GrainManage.Data.Model.Table;

namespace GrainManage.Server.Business
{
    class DistrictBusiness : BaseBusiness, GrainManage.Interface.IDistrict
    {
        //************************************************POST************************************************
        public OutputGetByID GetByID(InputGetByID input)
        {
            var result = new OutputGetByID();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                var table = service.GetByID(input.DistrictID);
                if (table != null)
                {
                    result.District = table;
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.NoData, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetByName GetByName(InputGetByName input)
        {
            var result = new OutputGetByName();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                result.Districts = service.GetByName(input.DistrictName, input, result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetFatherName GetFatherName(InputGetFatherName input)
        {
            var result = new OutputGetFatherName();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                result.DistrictName = service.GetFatherName(input.DistrictID);
                if (string.IsNullOrEmpty(result.DistrictName))
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetChildName GetChildName(InputGetChildName input)
        {
            var result = new OutputGetChildName();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                var data = service.GetChildName(input.DistrictID, input);
                result.DistrictNames = service.GetChildName(input.DistrictID, input);  
                if (result.DistrictNames.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetUpwardDistrict GetUpwardDistrict(InputGetUpwardDistrict input)
        {
            var result = new OutputGetUpwardDistrict();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                result.Districts = service.GetUpwardDistrict(input.Id, input.Level,result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetDownwardDistrict GetDownwardDistrict(InputGetDownwardDistrict input)
        {
            var result = new OutputGetDownwardDistrict();
            try
            {
                var service = ObjectFactory.GetDistricService(input);
                result.Districts = service.GetDownwardDistrict(input.Id, input.Level, input,result);
                if (result.Districts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }
    }
}
